//Heitor Paceli
//Rafael Araujo
package Objects;

import java.util.*;

public class Server {
    
    private long count;
    private long libraryCount;
    private HashMap clientHashMap = new HashMap();

    public HashMap getClientHashMap() {
        return clientHashMap;
    }

    public void setClientHashMap(HashMap clientHashMap) {
        this.clientHashMap = clientHashMap;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public long getLibraryCount() {
        return libraryCount;
    }

    public void setLibraryCount(long libraryCount) {
        this.libraryCount = libraryCount;
    }
    
    public Client registerNewClient(Library library) {
        
        Client client = new Client();
        
        Scanner in = new Scanner(System.in);
        String clientName = in.nextLine();
        
        client.setId(count);
        client.setName(clientName);
        client.setDebit(0.0);
        
        clientHashMap.put(client, library);
        count++;
        
        return client;
    }
    
    public Library registerNewLibrary(){
        
        Library library = new Library();
        Scanner in = new Scanner(System.in);
        String libraryName = in.nextLine();
        
        library.setId(libraryCount);
        library.setLibraryName(libraryName);
        
        libraryCount++;
        
        return library;
    }
    
    public Library getSourceLibrary(Client client) {

        Library tempLibrary = null;
        
        Iterator it = clientHashMap.keySet().iterator();
        
        while (it.hasNext()) {
            
            Client clientTemp = (Client) it.next();
            Library lib = (Library) clientHashMap.get(clientTemp);
            
            if (clientTemp.equals(client)){
                tempLibrary = lib;
            }
        }
        
        
        return tempLibrary;
    }
    
    
    //***********************************************************************************************
    public Client getClientById(Long id) {

        Client client = null;
        
        Iterator it = clientHashMap.keySet().iterator();
        
        while (it.hasNext()) {
            
            Client temp = (Client) it.next();
            
            if (temp.getId() == id){
                client = temp;
            }
        }
        
        return client;
    }
    //***********************************************************************************************
}
